package com.zjh.desensitive.holo;

import com.zjh.desensitive.DesensitiveMain;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.List;
import java.util.Map;

public class TestHolo {
    public static void main(String[] args) {
        Request request = new Request();
        request.setDbType("mysql");
        request.setDataType("string");
        request.setType("mask");
        request.setStart_index(3);
        request.setEnd_index(5);


        if(request.getDataType().equals("hologres")){
            StdExtConf stdExtConf = new StdExtConf();
            stdExtConf.setDbType(request.getDbType());
            stdExtConf.setDataType(request.getDataType());

            StdExtConfExpress express = new StdExtConfExpress();
            express.setType(request.getType());
            request.setStart_index(request.getStart_index());
            request.setEnd_index(request.getEnd_index());

            stdExtConf.setExpress(express);
            //TODO 与保护伞同步规则


        }else if(request.getDbType().equals("mysql")){
            String mysql_rule_template = "concat(concat(substring(${obj}, 1, ${start_index}), '******'), substring(${obj}, ${end_index}))";
            if(request.getStart_index() > 1){
                String start_index = String.valueOf(request.getStart_index());
                mysql_rule_template = mysql_rule_template.replaceAll("\\$\\{start_index\\}", start_index);
            }else{
                mysql_rule_template = mysql_rule_template.replaceAll("\\$\\{start_index\\}", "1");
            }

            if(request.getEnd_index() > request.getStart_index()){
                String end_index = String.valueOf(request.getEnd_index());
                mysql_rule_template = mysql_rule_template.replaceAll("\\$\\{end_index\\}", end_index);
            }else{
                mysql_rule_template = mysql_rule_template.replaceAll("\\$\\{end_index\\}", String.valueOf(request.getStart_index()));
            }

            StdExtConf stdExtConf = new StdExtConf();
            stdExtConf.setDbType(request.getDbType());
            stdExtConf.setDataType(request.getDataType());
            stdExtConf.setExpress(mysql_rule_template);
            //TODO 与安全平台同步规则

        }

    }


    private static void test_holo_1(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://hgpostcn-cn-7pp2r0lli009-cn-hangzhou.hologres.aliyuncs.com:80/test");
        dataSource.setUsername("LTAI4FvghyDBS89pWfcmWkW2");
        dataSource.setPassword("12r0mEuIh24trMHiPWwoXYShN3pngT");
        jdbcTemplate.setDataSource(dataSource);

        jdbcTemplate.update("SECURITY LABEL FOR hg_anon ON ROLE \"1278037536948614\" IS NULL;");

        String s = DesensitiveMain.readFile(
                "D:\\develop\\static-desensitive-util\\src\\main\\java\\com\\zjh\\desensitive\\holo.json");
        jdbcTemplate.update("ALTER DATABASE test SET hg_anon_labels = '" + s + "'");

        jdbcTemplate.update("SECURITY LABEL FOR hg_anon ON COLUMN test_db.name IS 'name'");
        List<Map<String, Object>> maps6 = jdbcTemplate.queryForList("select * from test_db");
        System.out.println(maps6);


        String s1 = DesensitiveMain.readFile(
                "D:\\develop\\static-desensitive-util\\src\\main\\java\\com\\zjh\\desensitive\\holo1.json");
        jdbcTemplate.update("ALTER DATABASE test SET hg_anon_labels = '" + s1 + "'");

        jdbcTemplate.update("SECURITY LABEL FOR hg_anon ON COLUMN test_db.name IS 'first_mask'");
        List<Map<String, Object>> maps5 = jdbcTemplate.queryForList("select * from test_db");
        System.out.println(maps5);
    }
}
